package com.xxx.interceptor;

import java.util.Properties;

import org.apache.ibatis.executor.statement.RoutingStatementHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Invocation;

/**
 * 自定义打印sql日志插件
 */
public class LogInterceptor implements Interceptor {
	
	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		System.out.println("LogInterceptor : intercept");
		return null;
	}

	@Override
	public Object plugin(Object target) {
		if (target instanceof StatementHandler) {
			RoutingStatementHandler handler = (RoutingStatementHandler) target;
			// 打印出当前执行的sql语句
			System.out.println("打印当前执行的sql语句 : " + handler.getBoundSql().getSql());
			// 打印参数
			System.out.println("打印当前执行的sql参数 : " + handler.getBoundSql().getParameterObject());
		}
		return target;
	}

	@Override
	public void setProperties(Properties properties) {
		System.out.println("LogInterceptor : setProperties");
	}
}
